package com.sudao.android.ball; 

import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Binder;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

import java.io.IOException;
import java.util.HashMap;

public class LocalService extends Service {

	private static final String LOG_CAT = LocalService.class.getSimpleName();

    static final int SOUND_WIN = 1;
    static final int SOUND_LOSE = 2;
    static final int SOUND_BALL_BOUNCE = 3;
    static final int SOUND_GET_PILL = 4;
    static final int SOUND_LASER_HIT_BRICK = 5;
    static final int SOUND_ROCKET_HIT_BRICK = 6;
    static final int SOUND_SHOOT_LASER = 7;
    static final int SOUND_SHOOT_ROCKET = 8;
    static final int SOUND_BALL_HIT_BRICK = 9;
    //static final int SOUND_BRICK_BROKE = 10;
    static final int SOUND_BOMB_EXPLODE = 11;

    private final IBinder mBinder = new LocalBinder();

    private SoundPool soundPool;
    private HashMap<Integer, Integer> soundPoolMap;
    private boolean mPlaySound;
    private float mPlaySoundVolume;

	private BackgroundMusic mBackgroundMusic;

    public class LocalBinder extends Binder {
        LocalService getService() {
            return LocalService.this;
        }
    }
    
    @Override
    public void onCreate() {
		initSounds();
		loadPreferences();
		//mBackgroundMusic = new BackgroundMusic();
    }

    @Override
    public void onDestroy() {
		//mBackgroundMusic.interruptThread();
		releaseSounds();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

	private void loadPreferences() {
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);

		boolean sound = pref.getBoolean(SoundControl.PREF_SOUND, SoundControl.DEFAULT_PREF_SOUND);
		float soundVolume = pref.getInt(SoundControl.PREF_SOUND_VOLUME, SoundControl.DEFAULT_PREF_SOUND_VOLUME) / 100f;
		int padSpeed = Integer.parseInt(pref.getString(SoundControl.PREF_PAD_SPEED, 
			SoundControl.DEFAULT_PREF_PAD_SPEED + ""));
		
		Log.d(LOG_CAT, "sound preferences : " + sound + " "+soundVolume);
		setPlaySound(sound);
		setPlaySoundVolume(soundVolume);
	}

    public void setPlaySound(boolean b) {
        mPlaySound = b;
    }

    public void setPlaySoundVolume(float f) {
        mPlaySoundVolume = f;
    }

    private void loadSound(AssetManager am, String name, int code) {
        /*try {
            AssetFileDescriptor afd = am.openFd(name);
            int id = soundPool.load(name, 1);
            soundPoolMap.put(code, id);
        } catch (IOException e1) {
            Log.d(LOG_CAT, "load sound resource error. resource name is:" + name);
        }*/
        int id = soundPool.load(name, 1);
        soundPoolMap.put(code, id);
    }

	public void initSounds() {
		Log.d(LOG_CAT, "initSounds");

		soundPool = new SoundPool(11, AudioManager.STREAM_MUSIC, 0);
        soundPoolMap = new HashMap<Integer, Integer>();
        AssetManager am = getAssets();
      
        loadSound(am, "ball_bounce.ogg", SOUND_BALL_BOUNCE);
        loadSound(am, "ball_hit_brick.ogg", SOUND_BALL_HIT_BRICK);
        loadSound(am, "bomb_explode.ogg", SOUND_BOMB_EXPLODE);
        //loadSound(am, "brick_broke.ogg", SOUND_BRICK_BROKE);
        loadSound(am, "get_pill.ogg", SOUND_GET_PILL);
        loadSound(am, "laser_hit_brick.ogg", SOUND_LASER_HIT_BRICK);
        loadSound(am, "lose.ogg", SOUND_LOSE);
        loadSound(am, "rocket_hit_brick.ogg", SOUND_ROCKET_HIT_BRICK);
        loadSound(am, "shoot_laser.ogg", SOUND_SHOOT_LASER);
        loadSound(am, "shoot_rocket.ogg", SOUND_SHOOT_ROCKET);
        loadSound(am, "win.ogg", SOUND_WIN);
	}

	public class BackgroundMusic extends Thread {
		private MediaPlayer mMediaPlayer;	
    
		public BackgroundMusic() {
			super();
			playBackgoundMusic();
			start();
		}

		public void run() {
			/*while(true) {
				Log.d("_________", "runing........");
			}*/
		}

		private void playBackgoundMusic() {
			mMediaPlayer = MediaPlayer.create(LocalService.this, R.raw.bgmusic);
			mMediaPlayer.setLooping(true);
			mMediaPlayer.start();
		}
    
		private void stopBackgroundMusic() {
			if (mMediaPlayer != null) {
				mMediaPlayer.release();
				mMediaPlayer = null;
			}
		}

		public void interruptThread() {
			stopBackgroundMusic();
			interrupt();
		}
	}

    public void playSound(int sound, boolean playSound) {
        if (!mPlaySound)
            return;
        if (!playSound)
            return;
        if (soundPool.play(soundPoolMap.get(sound), mPlaySoundVolume, mPlaySoundVolume, 0, 0, 1f) == 0) {
            Log.d("______", "load sound resource failed.");
        }
    }

    public void releaseSounds() {
        soundPool.release();
    }
	
}

